#include "systemsettings.h"
#include "ui_systemsettings.h"
#include <QPainter>
#include "inquiresql.h"
#include <QMessageBox>
#include "QKeyEvent"
//#include "systemset.h"

SystemSettings::SystemSettings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SystemSettings)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(555,535);
    setWindowTitle(tr("系统设置"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    ui->spinBoxIntegrationMode->setSuffix(tr("￥"));
    ui->spinBoxIntegrationMode->setRange(0,100);       //设置变化范围

    //功能未实现
    ui->checkBoxInventoryShortagesProhibitSale->setDisabled(true);
    ui->checkBoxPrintCommodityBarcode->setDisabled(true);
    ui->checkBoxPrintMemberIntegration->setDisabled(true);
    ui->checkBoxPrintSellingPrice->setDisabled(true);

    //添加时间过滤器
    QWidget::installEventFilter(this);
}

SystemSettings::~SystemSettings()
{
    delete ui;
}

void SystemSettings::paintEvent(QPaintEvent *)
{
    QPainter paint(this);
//    QBrush brush;
//    brush.setColor(QColor);
    QPen thickPen(QColor(0, 245, 255), 1.5);//设置刻度线为粗黑
    paint.setPen(thickPen);
    paint.drawLine(5,155,550,155);

    static const QPointF points[4] = {
        QPointF(5.0, 320.0),
        QPointF(554, 320.0),
        QPointF(554, 372.0),
        QPointF(5.0, 372.0)
    };
    QPainter painter(this);
    painter.setPen(thickPen);
    painter.drawConvexPolygon(points, 4);

    static const QPointF points2[4] = {
        QPointF(5.0, 382.0),
        QPointF(554, 382.0),
        QPointF(554, 465.0),
        QPointF(5.0, 465.0)
    };
    QPainter painter2(this);
    painter2.setPen(thickPen);
    painter2.drawConvexPolygon(points2, 4);
}

bool SystemSettings::eventFilter(QObject *target, QEvent *event)
{
    //键盘事件
    if (event->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Escape)
        {
            this->hide();
            return true;
        }
    }
    return QWidget::eventFilter(target, event);
}

void SystemSettings::displaySystemSettingsInformation()
{
    m_systemSet = m_sql->InquireSystemSettingsInformation();

    ui->lineEditTitle->setText(m_systemSet.get_title());
    ui->checkBoxInventoryShortagesProhibitSale->setCheckState(
                m_systemSet.get_inventoryShortagesProhibitSale()?Qt::Checked:Qt::Unchecked);
    ui->checkBoxInventoryShortagesPrompts->setCheckState(
                m_systemSet.get_inventoryShortagesPrompts()?Qt::Checked:Qt::Unchecked);
    ui->checkBoxUseMemberPassword->setCheckState(
                m_systemSet.get_useMemberPassword()?Qt::Checked:Qt::Unchecked);
    ui->spinBoxIntegrationMode->setValue(m_systemSet.get_integrationMode());

    ui->checkBoxForbidModifyPricesDiscounts->setCheckState(
                m_systemSet.get_forbidModifyPricesDiscounts()?Qt::Checked:Qt::Unchecked);
    ui->checkBoxPrintMemberIntegration->setCheckState(
                m_systemSet.get_printMemberIntegration()?Qt::Checked:Qt::Unchecked);
    ui->checkBoxModifyWindowPopsUp->setCheckState(
                m_systemSet.get_modifyWindowPopsUp()?Qt::Checked:Qt::Unchecked);
    ui->checkBoxPrintCommodityBarcode->setCheckState(
                m_systemSet.get_printCommodityBarcode()?Qt::Checked:Qt::Unchecked);
    ui->checkBoxAutoAddCommodityQuantity->setCheckState(
                m_systemSet.get_autoAddCommodityQuantity()?Qt::Checked:Qt::Unchecked);
    ui->checkBoxPrintSellingPrice->setCheckState(
                m_systemSet.get_printSellingPrice()?Qt::Checked:Qt::Unchecked);
    ui->checkBoxUseDeleteOrderFormProtection->setCheckState(
                m_systemSet.get_useDeleteOrderFormProtection()?Qt::Checked:Qt::Unchecked);
    ui->lineEditDeleteOrderFormPassword->setText(m_systemSet.get_deleteOrderFormPassword());
    ui->comboBoxMemberPrice->setCurrentIndex(m_systemSet.get_memberPrice());
    ui->comboBoxGeneralCustomerPrice->setCurrentIndex(m_systemSet.get_generalCustomerPrice());
}

void SystemSettings::on_pushButtonCancel_clicked()
{
    this->hide();
}

void SystemSettings::on_pushButtonSave_clicked()
{
    if (ui->lineEditTitle->text().isEmpty())
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n收银台标题不能为空，请重新添加标题!"));

    int inventoryShortagesProhibitSale  = (
                ui->checkBoxInventoryShortagesProhibitSale->isChecked())? 1 : 0;
    int inventoryShortagesPrompts  = (
                ui->checkBoxInventoryShortagesPrompts->isChecked())? 1 : 0;
    int useMemberPassword               = (
                ui->checkBoxUseMemberPassword->isChecked())? 1 : 0;
    int forbidModifyPricesDiscounts     = (
                ui->checkBoxForbidModifyPricesDiscounts->isChecked())? 1 : 0;
    int printMemberIntegration          = (
                ui->checkBoxPrintMemberIntegration->isChecked())? 1 : 0;
    int modifyWindowPopsUp              = (
                ui->checkBoxModifyWindowPopsUp->isChecked())? 1 : 0;
    int printCommodityBarcode           = (
                ui->checkBoxPrintCommodityBarcode->isChecked())? 1 : 0;
    int autoAddCommodityQuantity        = (
                ui->checkBoxAutoAddCommodityQuantity->isChecked())? 1 : 0;
    int printSellingPrice               = (
                ui->checkBoxPrintSellingPrice->isChecked())? 1 : 0;
    int useDeleteOrderFormProtection    = (
                ui->checkBoxUseDeleteOrderFormProtection->isChecked())? 1 : 0;

//    str.replace(QString("ou"), QString("o")); ￥
//    t.remove(QChar('a'), Qt::CaseInsensitive);

//    qDebug() << ui->spinBoxIntegrationMode->text().replace(QString("￥"), QString(""));
//    qDebug() << ui->spinBoxIntegrationMode->text().left(ui->spinBoxIntegrationMode->text().count() - 1);

    SystemSet systemSettingsInformation(ui->lineEditTitle->text(),
            inventoryShortagesProhibitSale,
            inventoryShortagesPrompts,
            useMemberPassword,
            ui->spinBoxIntegrationMode->text()
                    .left(ui->spinBoxIntegrationMode->text().count() - 1).toInt(),
            forbidModifyPricesDiscounts,
            printMemberIntegration,
            modifyWindowPopsUp,
            printCommodityBarcode,
            autoAddCommodityQuantity,
            printSellingPrice,
            useDeleteOrderFormProtection,
            ui->lineEditDeleteOrderFormPassword->text(),
            ui->comboBoxMemberPrice->currentIndex(),
            ui->comboBoxGeneralCustomerPrice->currentIndex());
    if (!m_sql->UpdateSystemSettingsInformation(systemSettingsInformation))
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n修改系统设置失败!"));
    else
    {
        emit updateSystemSettingsInformation();
        this->hide();
        emit modifySystemInformationSuccess();
//        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
//                             tr("\n修改系统设置成功!"));
    }
}
